<?php

namespace App\Controller\Home;

use App\Controller\Example\CtcCancelExample;
use App\Controller\Example\CtcExample;
use App\Controller\Example\CtcOrdersExample;
use App\Controller\Example\CtcSettingExample;
use App\Controller\Example\UserExample;
use App\Models\ConfigModel;
use App\Models\Conn;
use App\Models\CtcCancelModel;
use App\Models\CtcModel;
use App\Models\CtcOrdersModel;
use App\Models\CtcSettingModel;
use App\Models\UsersModel;
use App\Models\UsersPaymentModel;
use Config\Config;
use Core\Common\Common;
use Core\Enums\DigitEnum;
use Core\Enums\ErrorEnum;
use Core\Enums\FundsEnum;
use Core\Result\Result;
use Core\Utils\Utils;

class Ctc
{
    /**
     * TODO C2C产品列表
     * entry  token
     * return json
     * */
    public function ctcProductList($data){
        $conn = new Conn();
        $ctcList = CtcSettingModel::CtcSettingList($conn);
        foreach ($ctcList as $item => $value){
            $ctcList[$item]["ratio"] =  number_format(((($value["consult_price"] - $value["system_price"]) / $value["system_price"]) * 100),2) ." %";
        }
        Result::success($ctcList);
    }

    /**
     * TODO C2C订单列表
     * entry  token size wid
     * return json
     * */
    public function ctcList($data){
        $assemble = Common::assemble(new CtcExample(), $data);
        $conn = new Conn();
        $assemble->limit = DigitEnum::FIVE;
        $ctcBuyList = CtcModel::CtcBuyList($conn, $assemble);
        $ctcSellList = CtcModel::CtcSellList($conn, $assemble);
        $list['buy'] = $ctcBuyList;
        $list['sell'] = $ctcSellList;
        Result::success($list);
    }
    /**
     * TODO C2C订单详情
     * entry  token size wid price type
     * return json
     * */
    public function goodsDetailList($data){
        $assemble = Common::assemble(new CtcExample(), $data);
        $conn = new Conn();
        $assemble->limit = DigitEnum::TWENTY;
        $userInside = UsersModel::getUserInside($conn, $assemble->token);
        $assemble->uid = $userInside["id"];
        $ctDetailList = CtcModel::CtcDetailList($conn, $assemble);
        Result::success($ctDetailList);
    }
    /**
     * TODO C2C用户下单
     * entry  token id
     * return json
     * */
    function goodsOrderAdd($data){
        $assemble = Common::assemble(new UserExample(), $data);
        $conn = new Conn();
        $CtcExample = Common::assemble(new CtcExample(),CtcModel::getCtcDetailInit($conn, $assemble));
        if($CtcExample->status != DigitEnum::ONE || $CtcExample->id == null){
            Result::error(ErrorEnum::THISORDERHASEXPIRED[$assemble->language]);
        }
        $CtcSettingExample = Common::assemble(new CtcSettingExample(), CtcSettingModel::getCtcSettingInit($conn, $CtcExample->wid));
        if($CtcSettingExample->id == null ){
            Result::error(ErrorEnum::CURRENCYINFORMATIONDOESNOTEXIST[$assemble->language]);
        }
        $trade_begin_time = strtotime(date('Y-m-d') . ' ' . $CtcSettingExample->trade_begin_time);
        $trade_end_time = strtotime(date('Y-m-d') . ' ' . $CtcSettingExample->trade_end_time);
        $time = time();
        if ($CtcSettingExample->is_stop == DigitEnum::ZERO || $CtcSettingExample->is_stop_trade == DigitEnum::ZERO){
            Result::error(ErrorEnum::THECURRENCYHASSTOPPEDTRADING[$assemble->language]);
        }
        if ($time < $trade_begin_time || $time > $trade_end_time) Result::error(ErrorEnum::THECURRENCYHASSTOPPEDTRADING[$assemble->language]);
        $UserExample = Common::assemble(new UserExample(), UsersModel::getUserInside($conn, $assemble->token));//当前用户信息
        $userPasswordInside = UsersModel::getUserPasswordInside($conn, $assemble->token);
        if ($UserExample->id == null) {
            Result::error(ErrorEnum::ACCOUNTINVALID[$assemble->language]);
        }
        if ($userPasswordInside["pay_password"] == null) {
            Result::error(ErrorEnum::PLEASESETASECONDLEVELPAYMENTPASSWORD[$assemble->language]);
        }
        if ($UserExample->is_trade != DigitEnum::ONE) {
            Result::error(ErrorEnum::YOUHAVEBEENBANNEDFROMTRADING[$assemble->language]);
        }
        if ($UserExample->id == $CtcExample->uid) {
            Result::error(ErrorEnum::NONACTIONABLEORDERS[$assemble->language]);
        }
        $OrderUserExample = Common::assemble(new UserExample(), UsersModel::getUserInsideId($conn, $CtcExample->uid));//订单主信息
        $conn->shiwuStart();
        $ctcOrdersExample = new CtcOrdersExample();
        $ctcOrdersExample->sn = Utils::createOrderNo();
        $ctcOrdersExample->commission = $CtcExample->commission;
        if($CtcExample->type == DigitEnum::ONE){
            $ctcOrdersNum = CtcOrdersModel::getCtcOrdersNum($conn, $UserExample->id, $CtcExample->wid);
            $ctcNum = CtcModel::getCtcNum($conn, $UserExample->id, $CtcExample->wid,2);
            if($ctcOrdersNum + $ctcNum >= $CtcSettingExample->buy_num){
                Result::error(ErrorEnum::ORDERQUANTITYOPERATIONLIMIT[$assemble->language]);
            }
            $ctcOrdersExample->payment = $CtcExample->payment;
            $ctcOrdersExample->uid = $UserExample->id;
            $ctcOrdersExample->buy_name = $UserExample->truename;
            $ctcOrdersExample->buy_mobile = $UserExample->phone;
            $ctcOrdersExample->buy_white = $UserExample->is_white;
            $ctcOrdersExample->to_uid = $OrderUserExample->id;
            $ctcOrdersExample->sell_name = $OrderUserExample->truename;
            $ctcOrdersExample->sell_mobile = $OrderUserExample->phone;
            $ctcOrdersExample->sell_white = $OrderUserExample->is_white;
        }
        if($CtcExample->type == DigitEnum::TWO){
            $ctcOrdersNum2 = CtcOrdersModel::getCtcOrdersToNum($conn, $UserExample->id, $CtcExample->wid);
            $ctcNum2 = CtcModel::getCtcNum($conn, $UserExample->id, $CtcExample->wid,1);
            if($ctcOrdersNum2 + $ctcNum2 >= $CtcSettingExample->sell_num){
                Result::error(ErrorEnum::ORDERQUANTITYOPERATIONLIMIT[$assemble->language]);
            }
            if($CtcExample->wid == 2){
                $payMoney = $CtcExample->quantity;//用户支付消费积分
                $payMyr = round($CtcExample->quantity * $CtcExample->price * $CtcSettingExample->commission ,2);//用户支付交易积分
                $balanceMoney = $UserExample->money - $payMoney;
                $balanceMyr = $UserExample->myr - $payMyr;
                if($UserExample->money <= DigitEnum::ZERO || $balanceMoney < DigitEnum::ZERO || $payMoney < DigitEnum::ZERO){
                    Result::error(ErrorEnum::INSUFFICIENTBALANCE[$assemble->language]);
                }
                if($UserExample->myr <= DigitEnum::ZERO || $balanceMyr < DigitEnum::ZERO || $payMyr < DigitEnum::ZERO){
                    Result::error(ErrorEnum::INSUFFICIENTHANDLINGFEES[$assemble->language]);
                }
                $UserPaymentInside = UsersPaymentModel::QueryUserPaymentInside($conn, $UserExample->id);
                if(!$UserPaymentInside){
                    Result::error(ErrorEnum::PLEASEBINDTHEPAYMENTMETHODFIRST[$assemble->language]);
                }
                $payment = array_column($UserPaymentInside, 'payment');
                $payment = implode(',', $payment);
                $ctcOrdersExample->payment = $payment;
                $ctcOrdersExample->uid = $OrderUserExample->id;
                $ctcOrdersExample->buy_name = $OrderUserExample->truename;
                $ctcOrdersExample->buy_mobile = $OrderUserExample->phone;
                $ctcOrdersExample->buy_white = $OrderUserExample->is_white;
                $ctcOrdersExample->to_uid = $UserExample->id;
                $ctcOrdersExample->sell_name =  $UserExample->truename;
                $ctcOrdersExample->sell_mobile = $UserExample->phone;
                $ctcOrdersExample->sell_white = $UserExample->is_white;
                $editUserMoneyOutInside = UsersModel::editUserMoneyOutInside($conn, $payMoney,$payMyr, $UserExample);
                $writeFundsMoney = Funds::writeFundsMoney($conn, $UserExample->id, $ctcOrdersExample->sn, FundsEnum::CTCSELL, -$payMoney, $balanceMoney, FundsEnum::CTCSELLZHCN);
                $writeFundsMyr = Funds::writeFundsMyr($conn, $UserExample->id, $ctcOrdersExample->sn, FundsEnum::CTCSELL, -$payMyr, $balanceMyr, FundsEnum::CTCSELLFEEZHCN);
                if($editUserMoneyOutInside != DigitEnum::ONE || $writeFundsMoney != DigitEnum::ONE || $writeFundsMyr != DigitEnum::ONE){
                    $conn->shiwuRollback();
                    Result::error(ErrorEnum::ORDERPLACEMENTFAILED[$assemble->language]);
                }
            }else if($CtcExample->wid == 1){
                $payMyr = round($CtcExample->quantity * $CtcExample->price * $CtcSettingExample->commission ,2);//用户支付交易积分
                $payMoney = $CtcExample->quantity + $payMyr;//用户支付消费积分
                $balanceMoney = $UserExample->myr - $payMoney;
                if($UserExample->myr <= DigitEnum::ZERO || $balanceMoney < DigitEnum::ZERO || $payMoney < DigitEnum::ZERO){
                    Result::error(ErrorEnum::INSUFFICIENTBALANCE[$assemble->language]);
                }
                $UserPaymentInside = UsersPaymentModel::QueryUserPaymentInside($conn, $UserExample->id);
                if(!$UserPaymentInside){
                    Result::error(ErrorEnum::PLEASEBINDTHEPAYMENTMETHODFIRST[$assemble->language]);
                }
                $payment = array_column($UserPaymentInside, 'payment');
                $payment = implode(',', $payment);
                $ctcOrdersExample->payment = $payment;
                $ctcOrdersExample->uid = $OrderUserExample->id;
                $ctcOrdersExample->buy_name = $OrderUserExample->truename;
                $ctcOrdersExample->buy_mobile = $OrderUserExample->phone;
                $ctcOrdersExample->buy_white = $OrderUserExample->is_white;
                $ctcOrdersExample->to_uid = $UserExample->id;
                $ctcOrdersExample->sell_name =  $UserExample->truename;
                $ctcOrdersExample->sell_mobile = $UserExample->phone;
                $ctcOrdersExample->sell_white = $UserExample->is_white;
                $editUserMoneyOutInside = UsersModel::editUserMyrOutInside($conn, $payMoney, $UserExample);
                $writeFundsMoney = Funds::writeFundsMyr($conn, $UserExample->id, $ctcOrdersExample->sn, FundsEnum::CTCSELL, -$payMoney, $balanceMoney, FundsEnum::CTCSELLZHCN);
                if($editUserMoneyOutInside != DigitEnum::ONE || $writeFundsMoney != DigitEnum::ONE){
                    $conn->shiwuRollback();
                    Result::error(ErrorEnum::ORDERPLACEMENTFAILED[$assemble->language]);
                }
            }
        }
        $UpCtcExample = new CtcExample();
        $UpCtcExample->id = $CtcExample->id;
        $UpCtcExample->status = 2;
        $UpCtcExample->update_time = time();
        $upCtcInside = CtcModel::upCtcInside($conn, $UpCtcExample);
        $ctcOrdersExample->wid = $CtcExample->wid;
        $ctcOrdersExample->type = $CtcExample->type;
        $ctcOrdersExample->user_id = $UserExample->id;
        $ctcOrdersExample->goods_id = $CtcExample->id;
        $ctcOrdersExample->quantity = $CtcExample->quantity;
        $ctcOrdersExample->price = $CtcExample->price;
        $ctcOrdersExample->total_price = $CtcExample->total_price;
        $ctcOrdersExample->group_id = $CtcExample->group_id;
        $ctcOrdersExample->create_time = $ctcOrdersExample->update_time = $time;
        $ctcOrdersExample->commission_type = 1;
        $addCtcOrders = CtcOrdersModel::addCtcOrders($conn, $ctcOrdersExample);
        if($upCtcInside != DigitEnum::ONE || $addCtcOrders != DigitEnum::ONE){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::ORDERPLACEMENTFAILED[$assemble->language]);
        }
        $mysql_insert_id = $conn->mysql_insert_id();
        $conn->shiwuEnd();
        Vcode::TctSend($OrderUserExample->phone,$UserExample->phone,2);
        //Vcode::TctSend($UserExample->username,$OrderUserExample->username,2);
        exit(json_encode(array(
            "code"=>200,
            "msg"=>ErrorEnum::CONFIRMATION[$assemble->language],
            "data"=>['order_id' => $mysql_insert_id]
        ),JSON_UNESCAPED_UNICODE));
    }
    /**
     * TODO 广告发布信息
     * @param wid
     * @return void
     */
    function goodsSet($data){
        $assemble = Common::assemble(new CtcExample(), $data);
        $conn = new Conn();
        $userInside = Common::assemble(new UserExample(),UsersModel::getUserInside($conn, $assemble->token));
        $ctcSettingInit = CtcSettingModel::getCtcSettingInit($conn, $assemble->wid);
        if(!$ctcSettingInit){
            Result::success(ErrorEnum::CURRENCYINFORMATIONDOESNOTEXIST[$assemble->language]);
        }
        $queryUserPaymentInside = UsersPaymentModel::QueryUserPaymentInside($conn, $userInside->id,$assemble->wid);
        $UserPaymentInside = null;
        if($queryUserPaymentInside){
            $UserPaymentInside = array_column($queryUserPaymentInside, 'payment');
        }
        $ctcSettingInit['transaction_amount'] = str_replace(" ", "", $ctcSettingInit['transaction_amount']);
        $ctcSettingInit['transaction_amount'] = explode(',', $ctcSettingInit['transaction_amount']);
        if($assemble->wid ==2){
            $res['balance'] = $userInside->money ?? 0;
        }else if($assemble->wid ==6){
            $res['balance'] = $userInside->usdt ?? 0;
        }else{
            $res['balance'] = $userInside->myr ?? 0;
        }
        $res['coin'] = $ctcSettingInit;
        $res['payment'] = $UserPaymentInside;
        Result::success($res);
    }
    /**
     * TODO 广告发布
     * @param wid, type, quantity, price, payment
     * @return void
     */
    function goodsAdd($data){
        $assemble = Common::assemble(new CtcExample(), $data);
        if($assemble->price <= DigitEnum::ZERO){
            Result::error(ErrorEnum::PRICEILLEGAL[$assemble->language]);
        }
        if($assemble->quantity <= DigitEnum::ZERO){
            Result::error(ErrorEnum::ILLEGALQUANTITY[$assemble->language]);
        }
        if (empty($assemble->payment)){
            Result::error(ErrorEnum::PLEASEBINDTHEPAYMENTMETHODFIRST[$assemble->language]);
        }
        $conn = new Conn();
        $CtcSettingExample = Common::assemble(new CtcSettingExample(), CtcSettingModel::getCtcSettingInit($conn, $assemble->wid));
        if($CtcSettingExample->id == null){
            Result::error(ErrorEnum::CURRENCYINFORMATIONDOESNOTEXIST[$assemble->language]);
        }
        $transaction_amount = explode(',', $CtcSettingExample->transaction_amount);
        if (!in_array($assemble->quantity, $transaction_amount)){
            Result::error(ErrorEnum::ILLEGALQUANTITY[$assemble->language]);
        }
        if (fmod($assemble->quantity, 200) != DigitEnum::ZERO) {
            Result::error("数量只能为200的倍数");
        }
        if($CtcSettingExample->is_stop_pub == DigitEnum::ZERO || $CtcSettingExample->is_stop == DigitEnum::ZERO){
            Result::error(ErrorEnum::THECURRENCYHASSTOPPEDTRADING[$assemble->language]);
        }
        $time = time();
        $trade_begin_time = strtotime(date('Y-m-d') . ' ' .$CtcSettingExample->trade_begin_time);
        $trade_end_time = strtotime(date('Y-m-d') . ' ' . $CtcSettingExample->trade_end_time);
        if($time < $trade_begin_time || $time > $trade_end_time){
            Result::error(ErrorEnum::NONTRADINGTIME[$assemble->language].$CtcSettingExample->trade_begin_time."-".$CtcSettingExample->trade_end_time);
        }
        if ($assemble->price < $CtcSettingExample->min_price || $assemble->price > $CtcSettingExample->max_price){
            Result::error(ErrorEnum::PRICEOVERRUN[$assemble->language]);
        }
        $UserExample = Common::assemble(new UserExample(), UsersModel::getUserInside($conn, $assemble->token));
        $userPasswordInside = UsersModel::getuserPasswordInside($conn, $assemble->token);
        if($assemble->type == 1 && $userPasswordInside["pay_password"] == null) {
            Result::error(ErrorEnum::PLEASESETASECONDLEVELPAYMENTPASSWORD[$assemble->language]);
        }
        if($UserExample->is_trade != DigitEnum::ONE){
            Result::error(ErrorEnum::YOUHAVEBEENBANNEDFROMTRADING[$assemble->language]);
        }
        $ctcExample = new CtcExample();
        $ctcExample->uid = $UserExample->id;
        $ctcExample->wid = $assemble->wid;
        $ctcExample->type = $assemble->type;
        $ctcExample->price = $assemble->price;
        $ctcExample->is_white = $UserExample->is_white;
        $ctcExample->quantity = $assemble->quantity;
        $ctcExample->group_id = $UserExample->group;
        $ctcExample->status = 1;
        $ctcExample->verify = 1;
        $ctcExample->create_time = $ctcExample->update_time = $time;
        $ctcExample->payment = $assemble->payment;
        $ctcExample->total_price = round(($assemble->quantity * $assemble->price),4);
        $ctcExample->commission = round($assemble->quantity * $assemble->price * $CtcSettingExample->commission ,2);//金本位手续费
        $sn = Utils::createOrderNo();
        $conn->shiwuStart();
        switch ($assemble->type){
            case 1:
                $ctcOrdersNum = CtcOrdersModel::getCtcOrdersNum($conn, $UserExample->id, $assemble->wid);
                $ctcNum = CtcModel::getCtcNum($conn, $UserExample->id, $assemble->wid,1);
                if($ctcOrdersNum + $ctcNum >= $CtcSettingExample->sell_num){
                    Result::error(ErrorEnum::ORDERQUANTITYOPERATIONLIMIT[$assemble->language]);
                }
                if($UserExample->is_white == DigitEnum::ONE){
                    $ctcExample->commission = 0;
                }
                $payMoney = $assemble->quantity;//用户支付消费积分
                $payMyr = $ctcExample->commission;//用户支付交易积分
                $balanceMyr = $UserExample->myr - $payMyr;
                if($assemble->wid == DigitEnum::TWO){
                    $balanceMoney = $UserExample->money - $payMoney;
                    if($UserExample->money <= DigitEnum::ZERO || $balanceMoney < DigitEnum::ZERO || $payMoney < DigitEnum::ZERO){
                        Result::error(ErrorEnum::INSUFFICIENTBALANCE[$assemble->language]);
                    }
                    $editUserMoneyOutInside = UsersModel::editUserMoneyOutInside($conn, $payMoney,$payMyr, $UserExample);
                    $writeFundsMoney = Funds::writeFundsMoney($conn, $UserExample->id, $sn, FundsEnum::CTCSELL, -$payMoney, $balanceMoney, FundsEnum::CTCSELLZHCN);
                    if($editUserMoneyOutInside != DigitEnum::ONE || $writeFundsMoney != DigitEnum::ONE){
                        $conn->shiwuRollback();
                        Result::error(ErrorEnum::PUBLISHINGFAILED[$assemble->language]);
                    }
                }else if($assemble->wid == 6){
                    $balanceUsdtMoney = $UserExample->usdt - $payMoney;
                    if($UserExample->usdt <= DigitEnum::ZERO || $balanceUsdtMoney < DigitEnum::ZERO || $payMoney < DigitEnum::ZERO){
                        Result::error(ErrorEnum::INSUFFICIENTBALANCE[$assemble->language]);
                    }
                    $editUserMoneyOutInside = UsersModel::editUserUsdtOutInside($conn, $payMoney, $UserExample);
                    $writeFundsMoney = Funds::writeFundsUsdt($conn, $UserExample->id, $sn, FundsEnum::CTCSELL, -$payMoney, $balanceUsdtMoney, FundsEnum::CTCSELLZHCN);
                    if($editUserMoneyOutInside != DigitEnum::ONE || $writeFundsMoney != DigitEnum::ONE){
                        $conn->shiwuRollback();
                        Result::error(ErrorEnum::PUBLISHINGFAILED[$assemble->language]);
                    }
                }else{
                    $balanceMoney = $UserExample->myr - $payMoney;
                    if($UserExample->myr <= DigitEnum::ZERO || $balanceMoney < DigitEnum::ZERO || $payMoney < DigitEnum::ZERO){
                        Result::error(ErrorEnum::INSUFFICIENTBALANCE[$assemble->language]);
                    }
                    $editUserMoneyOutInside = UsersModel::editUserMyrOutInside($conn, $payMoney, $UserExample);
                    $writeFundsMoney = Funds::writeFundsMyr($conn, $UserExample->id, $sn, FundsEnum::CTCSELL, -$payMoney, $balanceMoney, FundsEnum::CTCSELLZHCN);
                    if($editUserMoneyOutInside != DigitEnum::ONE || $writeFundsMoney != DigitEnum::ONE){
                        $conn->shiwuRollback();
                        Result::error(ErrorEnum::PUBLISHINGFAILED[$assemble->language]);
                    }
                }
                if($payMyr > DigitEnum::ZERO){
                    if($UserExample->myr <= DigitEnum::ZERO || $balanceMyr < DigitEnum::ZERO || $payMyr < DigitEnum::ZERO){
                        Result::error(ErrorEnum::INSUFFICIENTHANDLINGFEES[$assemble->language]);
                    }
                    $writeFundsMyr = Funds::writeFundsMyr($conn, $UserExample->id, $sn, FundsEnum::CTCSELL, -$payMyr, $balanceMyr, FundsEnum::CTCSELLFEEZHCN);
                    if($writeFundsMyr != DigitEnum::ONE){
                        $conn->shiwuRollback();
                        Result::error(ErrorEnum::PUBLISHINGFAILED[$assemble->language]);
                    }
                }
                break;
            case 2:
                $ctcOrdersNum2 = CtcOrdersModel::getCtcOrdersNum($conn, $UserExample->id, $assemble->wid);
                $ctcNum2 = CtcModel::getCtcNum($conn, $UserExample->id, $assemble->wid,2);
                if($ctcOrdersNum2 + $ctcNum2 >= $CtcSettingExample->buy_num){
                    Result::error(ErrorEnum::ORDERQUANTITYOPERATIONLIMIT[$assemble->language]);
                }
                break;
            default:
                Result::error(ErrorEnum::THECURRENCYHASSTOPPEDTRADING[$assemble->language]);
                break;
        }
        $orderMate = self::orderMate($conn, $ctcExample, $sn, $assemble->language);
        if($UserExample->is_white == DigitEnum::ONE){
            $ctcExample->create_time = $time - (60 * 60 * 24 * 30);
        }
        if($orderMate)$ctcExample->status = 2;
        if(!$orderMate){
            $addCtc = CtcModel::addCtc($conn, $ctcExample);
            if($addCtc != DigitEnum::ONE){
                $conn->shiwuRollback();
                Result::error(ErrorEnum::PUBLISHINGFAILED[$assemble->language]);
            }
        }
        $conn->shiwuEnd();
        if($orderMate){
            Result::success(ErrorEnum::MATCHINGSUCCESSFUL[$assemble->language]);
        }
        Result::success(ErrorEnum::OPERATIONSUCCESSFUL[$assemble->language]);
    }
    /**
     * TODO 广告匹配
     * @param $conn,$ctcExample
     * @return false|void
     */
    public function orderMate($conn,$CtcExample,$sn,$language){
        $time = time();
        $payment = explode(",",$CtcExample->payment);
        foreach ($payment as $value){
            if ($CtcExample->type == 1) {//卖单匹配买单
                $where = " `uid` <> $CtcExample->uid and `wid` = $CtcExample->wid and `type` = 2 and `quantity` = $CtcExample->quantity and `status`=1 and `price` >= $CtcExample->price and `group_id` = $CtcExample->group_id and `payment` regexp '(^|,)($value)(,|$)'";
                $order = "order by `price` desc,`create_time` asc";
            }
            if ($CtcExample->type == 2) {//买单匹配卖单
                $where = " `uid` <> $CtcExample->uid and `wid` = $CtcExample->wid and `type`=1 and `quantity` = $CtcExample->quantity and `status` = 1 and `price` <= $CtcExample->price and `group_id` = $CtcExample->group_id and `payment` regexp '(^|,)($value)(,|$)'";
                $order = "order by `price` asc,`create_time` asc";
            }
            $sql = "select `id`,`uid`,`wid`,`type`,`price`,`total_price`,`quantity`,`commission`,`commission_type`,`group_id`,`payment`,`status` from `ctc` where $where $order limit 1";
            $readConn = $conn->readConn($sql);
            if($readConn)break;
        }
        if(!$readConn) return false;
        $UserExample = Common::assemble(new UserExample(), UsersModel::getUserInsideId($conn, $CtcExample->uid));
        $ctcOrdersExample = new CtcOrdersExample();
        $ctcOrdersExample->payment = $CtcExample->payment;
        $ctcOrdersExample->sn = $sn;
        $ctcOrdersExample->commission = $CtcExample->commission;
        $CtcExample = Common::assemble(new CtcExample(), $readConn);
        $OrderUserExample = Common::assemble(new UserExample(), UsersModel::getUserInsideId($conn, $CtcExample->uid));
        if($CtcExample->type == DigitEnum::ONE){
            $ctcOrdersExample->uid = $UserExample->id;
            $ctcOrdersExample->buy_name = $UserExample->truename;
            $ctcOrdersExample->buy_mobile = $UserExample->phone;
            $ctcOrdersExample->buy_white = $UserExample->is_white;
            $ctcOrdersExample->to_uid = $OrderUserExample->id;
            $ctcOrdersExample->sell_name = $OrderUserExample->truename;
            $ctcOrdersExample->sell_mobile = $OrderUserExample->phone;
            $ctcOrdersExample->sell_white = $OrderUserExample->is_white;
        }
        if($CtcExample->type == DigitEnum::TWO){
            $ctcOrdersExample->uid = $OrderUserExample->id;
            $ctcOrdersExample->buy_name = $OrderUserExample->truename;
            $ctcOrdersExample->buy_mobile = $OrderUserExample->phone;
            $ctcOrdersExample->buy_white = $OrderUserExample->is_white;
            $ctcOrdersExample->to_uid = $UserExample->id;
            $ctcOrdersExample->sell_name =  $UserExample->truename;
            $ctcOrdersExample->sell_mobile = $UserExample->phone;
            $ctcOrdersExample->sell_white = $UserExample->is_white;
        }
        $UpCtcExample = new CtcExample();
        $UpCtcExample->id = $CtcExample->id;
        $UpCtcExample->status = 2;
        $UpCtcExample->update_time = time();
        $upCtcInside = CtcModel::upCtcInside($conn, $UpCtcExample);
        $ctcOrdersExample->wid = $CtcExample->wid;
        $ctcOrdersExample->type = $CtcExample->type;
        $ctcOrdersExample->user_id = $UserExample->id;
        $ctcOrdersExample->goods_id = $CtcExample->id;
        $ctcOrdersExample->quantity = $CtcExample->quantity;
        $ctcOrdersExample->price = $CtcExample->price;
        $ctcOrdersExample->total_price = $CtcExample->total_price;
        $ctcOrdersExample->group_id = $CtcExample->group_id;
        $ctcOrdersExample->create_time = $ctcOrdersExample->update_time = $time;
        $ctcOrdersExample->commission_type = 2;
        $addCtcOrders = CtcOrdersModel::addCtcOrders($conn, $ctcOrdersExample);
        if($upCtcInside != DigitEnum::ONE || $addCtcOrders != DigitEnum::ONE){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::ORDERPLACEMENTFAILED[$language]);
        }
        Vcode::TctSend($OrderUserExample->phone,$UserExample->phone,2);
        //Vcode::TctSend($UserExample->username,$OrderUserExample->username,2);
        return true;
    }
    /**
     *TODO 我的广告列表
     * @param $data page limit status
     * @return void
     */
    function myAdList($data){
        $assemble = Common::assemble(new CtcExample(), $data);
        $identical = Common::identical($data);
        $conn = new Conn();
        $userInside = UsersModel::getUserInside($conn, $assemble->token);
        $assemble->uid = $userInside["id"];
        $ctcExample = new CtcExample();
        switch ($assemble->status){
            case 1:
                $ctcUserList = CtcModel::getCtcUserList($conn, $assemble,$identical);
                break;
            case 2:
                $ctcOrdersUidToUid = CtcOrdersModel::getCtcOrdersUidToUid($conn, $assemble, $identical);
                foreach ($ctcOrdersUidToUid as $value){
                    $ctcExample->id = $value["goods_id"];
                    $ctcDetailInit = CtcModel::getCtcDetailInit($conn, $ctcExample);
                    $ctcDetailInit["order_status"] = $value["status"];
                    $ctcDetailInit["pay_status"] = $value["pay_status"];
                    $ctcDetailInit["appeal"] = $value["appeal"];
                    $ctcDetailInit["order_create_time"] = $value["create_time"];
                    $ctcUserList[] = $ctcDetailInit;
                }
                break;
            case 3:
                $ctcOrdersUidToUid = CtcOrdersModel::getCtcOrdersUidToUidComplete($conn, $assemble, $identical);
                foreach ($ctcOrdersUidToUid as $value){
                    $ctcExample->id = $value["goods_id"];
                    $ctcDetailInit = CtcModel::getCtcDetailInit($conn, $ctcExample);
                    $ctcDetailInit["order_status"] = $value["status"];
                    $ctcDetailInit["pay_status"] = $value["pay_status"];
                    $ctcDetailInit["appeal"] = $value["appeal"];
                    $ctcDetailInit["order_create_time"] = $value["create_time"];
                    $ctcUserList[] = $ctcDetailInit;
                }
                break;
            default:$ctcUserList = [];break;
        }
        foreach ($ctcUserList as $item => $value){
            $ctcSettingInit = CtcSettingModel::getCtcSettingInit($conn, $value["wid"]);
            $ctcUserList[$item]["wid_name"] = $ctcSettingInit["name"];
            $ctcUserList[$item]["wid_logo"] = $ctcSettingInit["path"];
            $ctcUserList[$item]["order_status"] = $value["order_status"];
            $ctcUserList[$item]["pay_status"] = $value["pay_status"];
            $ctcUserList[$item]["appeal"] = $value["appeal"];
            if($value['order_create_time'] >0)$ctcUserList[$item]['order_create_time'] = Utils::dateTime($value['order_create_time']);
            if($value['create_time'] >0)$ctcUserList[$item]['create_time'] = Utils::dateTime($value['create_time']);
            if($value['finnshed_time'] >0) $ctcUserList[$item]['finnshed_time'] = Utils::dateTime($value['finnshed_time']);
            if($value['update_time'] >0) $ctcUserList[$item]['update_time'] = Utils::dateTime($value['update_time']);
        }
        Result::success($ctcUserList);
    }
    /**
     * TODO 下架广告
     * @param $data id
     * @return void
     */
    function adOffShelf($data){
        $assemble = Common::assemble(new CtcExample(), $data);
        $conn = new Conn();
        $UserExample = Common::assemble(new UserExample(), UsersModel::getUserInside($conn, $assemble->token));
        $assemble->uid = $UserExample->id;
        $CtcExample = Common::assemble(new CtcExample(),CtcModel::getCtcDetailInUid($conn, $assemble));
        if($CtcExample->id == null){
            Result::error(ErrorEnum::THISORDERHASEXPIRED[$assemble->language]);
        }
        if($CtcExample->status != DigitEnum::ONE){
            Result::error(ErrorEnum::NONACTIONABLEORDERS[$assemble->language]);
        }
        $conn->shiwuStart();
        $ctcExample = new CtcExample();
        $ctcExample->id = $CtcExample->id;
        $ctcExample->status = '0';
        if(CtcModel::upCtcInside($conn,$ctcExample) != DigitEnum::ONE){
            Result::error(ErrorEnum::DELISTINGFAILED[$assemble->language]);
        }
        $sn = Utils::createOrderNo();
        if($CtcExample->type == 1){
            $totalMoney = $CtcExample->quantity;
            $totalMye = $CtcExample->commission;
            if($CtcExample->wid == DigitEnum::TWO){
                $balance = $UserExample->money + $totalMoney;
                $balanceMyr = $UserExample->myr + $totalMye;
                $editUserMoneyAndusdtInside = UsersModel::editUserMoneyAddInside($conn, $totalMoney, $UserExample);
                $editUserMyrAndusdtInside = UsersModel::editUserMyrAddInside($conn, $totalMye, $UserExample);
                $writeFundsMoney = Funds::writeFundsMoney($conn, $UserExample->id, $sn, FundsEnum::CTCOFFSHELF, $totalMoney, $balance, FundsEnum::CTCOFFSHELFZHCN);
                $writeFundsMyr = Funds::writeFundsMyr($conn, $UserExample->id, $sn, FundsEnum::CTCOFFSHELF, $totalMye, $balanceMyr, FundsEnum::CTCOFFSHELFZHCN);
                if($editUserMoneyAndusdtInside != DigitEnum::ONE || $editUserMyrAndusdtInside != DigitEnum::ONE || $writeFundsMoney != DigitEnum::ONE || $writeFundsMyr != DigitEnum::ONE){
                    $conn->shiwuRollback();
                    Result::error(ErrorEnum::DELISTINGFAILED[$assemble->language]);
                }
            }else if($CtcExample->wid == 6){
                $balance = $UserExample->usdt + $totalMoney;
                $editUserMoneyAndusdtInside = UsersModel::editUserUsdtAddInside($conn, $totalMoney, $UserExample);
                $writeFundsMoney = Funds::writeFundsUsdt($conn, $UserExample->id, $sn, FundsEnum::CTCOFFSHELF, $totalMoney, $balance, FundsEnum::CTCOFFSHELFZHCN);
                if($editUserMoneyAndusdtInside != DigitEnum::ONE || $writeFundsMoney != DigitEnum::ONE){
                    $conn->shiwuRollback();
                    Result::error(ErrorEnum::DELISTINGFAILED[$assemble->language]);
                }
            }else{
                $balance = $UserExample->myr + $totalMoney;
                $editUserMoneyAndusdtInside = UsersModel::editUserMyrAddInside($conn, $totalMoney, $UserExample);
                $writeFundsMyr = Funds::writeFundsMyr($conn, $UserExample->id, $sn, FundsEnum::CTCOFFSHELF, $totalMoney, $balance, FundsEnum::CTCOFFSHELFZHCN);
                if($editUserMoneyAndusdtInside != DigitEnum::ONE || $writeFundsMyr != DigitEnum::ONE){
                    $conn->shiwuRollback();
                    Result::error(ErrorEnum::DELISTINGFAILED[$assemble->language]);
                }
            }
        }
        $conn->shiwuEnd();
        Result::success(ErrorEnum::OPERATIONSUCCESSFUL[$assemble->language]);
    }
    /**
     *TODO 订单详情
     * @param $data id
     * @return void void
     */
    function orderInfo($data){
        $assemble = Common::assemble(new CtcExample(), $data);
        $conn = new Conn();
        $UserExample = Common::assemble(new UserExample(), UsersModel::getUserInside($conn, $assemble->token));
        $ctcOrdersExample = new CtcOrdersExample();
        $ctcOrdersExample->uid = $UserExample->id;
        $ctcOrdersExample->goods_id = $assemble->id;
        $ctcOrders = CtcOrdersModel::getCtcOrders($conn, $ctcOrdersExample);
        if(empty($ctcOrders["buy_mobile"])){
            $ctcOrders["buy_mobile"] = UsersPaymentModel::getUsersPaymentOne($conn,$ctcOrders["uid"]);
        }
        if(empty($ctcOrders["buy_name"])){
            $ctcOrders["buy_name"] = UsersPaymentModel::getUsersPaymentName($conn,$ctcOrders["uid"]);
        }
        if(empty($ctcOrders["sell_mobile"])){
            $ctcOrders["sell_mobile"] = UsersPaymentModel::getUsersPaymentOne($conn,$ctcOrders["to_uid"]);
        }
        if(empty($ctcOrders["sell_name"])){
            $ctcOrders["sell_name"] = UsersPaymentModel::getUsersPaymentName($conn,$ctcOrders["to_uid"]);
        }
        $ctcSettingInit = CtcSettingModel::getCtcSettingInit($conn, $ctcOrders["wid"]);
        $ctcOrders["order_status"] = $ctcOrders["status"];
        $ctcOrders["name"] = $ctcSettingInit["name"];
        $ctcOrders["coin_logo"] = $ctcSettingInit["path"];
        $ctcOrders["payment"] = UsersPaymentModel::QueryUserOrderPaymentInside($conn, $ctcOrders["to_uid"], $ctcOrders["payment"]);
        $CTC_CANCEL_CONGEAL_NUM = ConfigModel::getConfigSingleValName($conn, Config::CTC_CANCEL_CONGEAL_NUM);
        $CTC_CANCEL_CONGEAL_TIME = ConfigModel::getConfigSingleValName($conn, Config::CTC_CANCEL_CONGEAL_TIME);
        $day = $CTC_CANCEL_CONGEAL_TIME / 60 / 60 / 24;
        if($assemble->language == 0){
            $ctcOrders["prompt"] = "当周取消".$CTC_CANCEL_CONGEAL_NUM."次及以上将冻结".$day."天交易";
        }else{
            $ctcOrders["prompt"] = "Canceling ".$CTC_CANCEL_CONGEAL_NUM." or more times a week will freeze transactions for ".$day." days";
        }
        Result::success($ctcOrders);
    }
    /**
     *TODO 取消订单(买家可取消)
     * @param $data id
     * @return void
     */
    function orderCancel($data){
        $assemble = Common::assemble(new CtcOrdersExample(), $data);
        if($assemble->id == null){
            Result::error(ErrorEnum::THISORDERHASEXPIRED[$assemble->language]);
        }
        $conn = new Conn();
        $userInside = UsersModel::getUserInside($conn, $assemble->token);
        $assemble->uid = $userInside["id"];
        $ctcCancelOrders = CtcOrdersModel::getCtcCancelOrders($conn, $assemble);
        if(!$ctcCancelOrders){
            Result::error(ErrorEnum::NONACTIONABLEORDERS[$assemble->language]);
        }
        $assemble->status = '0';
        $assemble->appeal = '0';
        $assemble->update_time = time();
        $week = date("YW",time());
        $geCtcCancelInside = CtcCancelModel::geCtcCancelInside($conn, $assemble->uid, $week);
        $ctcCancelExample = new CtcCancelExample();
        $to_uid = $ctcCancelOrders["to_uid"];
        $userInsideId = UsersModel::getUserInsideId($conn, $to_uid);
        $conn->shiwuStart();
        if($geCtcCancelInside){
            $ctcCancelExample->id = $geCtcCancelInside["id"];
            $ctcCancelExample->num = $geCtcCancelInside["num"] +1;
            $CTC_CANCEL_CONGEAL_NUM = ConfigModel::getConfigSingleValName($conn, Config::CTC_CANCEL_CONGEAL_NUM);
            $CTC_CANCEL_CONGEAL_TIME = ConfigModel::getConfigSingleValName($conn, Config::CTC_CANCEL_CONGEAL_TIME);
            if($ctcCancelExample->num >= $CTC_CANCEL_CONGEAL_NUM){
                $ctcCancelExample->state = DigitEnum::TWO;
                $ctcCancelExample->end_time = time() + $CTC_CANCEL_CONGEAL_TIME;
            }
            $CtcCancelSql = CtcCancelModel::upCtcCancelInside($conn, $ctcCancelExample);
            if($userInside["is_trade"] == 1){
                $userExample = new UserExample();
                $userExample->token = $userInside["token"];
                $userExample->is_trade = '0';
                if(UsersModel::upUserInside($conn, $userExample) != DigitEnum::ONE){
                    $conn->shiwuRollback();
                    Result::error(ErrorEnum::CANCELFAILED[$assemble->language]);
                }
            }
        }else{
            $ctcCancelExample->uid = $assemble->uid;
            $ctcCancelExample->week = $week;
            $CtcCancelSql = CtcCancelModel::insetCtcCancel($conn, $ctcCancelExample);
        }
        if($CtcCancelSql != DigitEnum::ONE){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::CANCELFAILED[$assemble->language]);
        }
        $upCtcOrdersInside = CtcOrdersModel::upCtcOrdersInside($conn, $assemble);
        $ctcExample = new CtcExample();
        $ctcExample->id = $ctcCancelOrders["goods_id"];
        $ctcExample->status = '0';
        $upCtcInside = CtcModel::upCtcInside($conn, $ctcExample);
        $quantity = $ctcCancelOrders["quantity"];
        $commission = $ctcCancelOrders["commission"];
        $balanceMyr = $userInsideId["myr"] + $commission;
        if($ctcCancelOrders["wid"] == 2){
            $balance = $userInsideId["money"] + $quantity;
            $editUserMoneyAddInside = UsersModel::editUserMoneyAddInside($conn, $quantity, Common::assemble(new UserExample(), $userInsideId));
            $editUserMyrAddInside = UsersModel::editUserMyrAddInside($conn, $commission, Common::assemble(new UserExample(), $userInsideId));
            $writeFundsMoney = Funds::writeFundsMoney($conn, $to_uid, $ctcCancelOrders["sn"], FundsEnum::CTCCANCELLATION, $quantity, $balance, FundsEnum::CTCCANCELLATIONZHCN);
            $writeFundsMyr = Funds::writeFundsMyr($conn, $to_uid, $ctcCancelOrders["sn"], FundsEnum::CTCCANCELLATION, $commission, $balanceMyr, FundsEnum::CTCCANCELLATIONZHCN);
        }else if($ctcCancelOrders["wid"] == 6){
            $balance = $userInsideId["usdt"] + $quantity;
            $editUserMoneyAddInside = UsersModel::editUserUsdtAddInside($conn, $quantity, Common::assemble(new UserExample(), $userInsideId));
            $editUserMyrAddInside = 1;
            $writeFundsMoney = Funds::writeFundsUsdt($conn, $to_uid, $ctcCancelOrders["sn"], FundsEnum::CTCCANCELLATION, $quantity, $balance, FundsEnum::CTCCANCELLATIONZHCN);
            $writeFundsMyr = 1;
        }else{
            $balance = $userInsideId["myr"] + $quantity + $commission;
            $editUserMoneyAddInside = UsersModel::editUserMyrAddInside($conn, $quantity, Common::assemble(new UserExample(), $userInsideId));
            $writeFundsMoney = Funds::writeFundsMyr($conn, $to_uid, $ctcCancelOrders["sn"], FundsEnum::CTCCANCELLATION, $quantity, $balance, FundsEnum::CTCCANCELLATIONZHCN);
            $editUserMyrAddInside = 1;
            $writeFundsMyr = 1;
        }
        if($upCtcOrdersInside != DigitEnum::ONE || $editUserMyrAddInside != DigitEnum::ONE || $upCtcInside != DigitEnum::ONE || $editUserMoneyAddInside != DigitEnum::ONE || $writeFundsMoney != DigitEnum::ONE || $writeFundsMyr != DigitEnum::ONE){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::CANCELFAILED[$assemble->language]);
        }
        $conn->shiwuEnd();
        Result::success(ErrorEnum::OPERATIONSUCCESSFUL[$assemble->language]);
    }
    /**
     *TODO 确认支付
     * @param $data id ,certificate,pay_type
     * @return void
     */
    function orderPayment($data){
        $assemble = Common::assemble(new CtcOrdersExample(), $data);
        if($assemble->id == null){
            Result::error(ErrorEnum::THISORDERHASEXPIRED[$assemble->language]);
        }
        $conn = new Conn();
        $userInside = UsersModel::getUserInside($conn, $assemble->token);
        $assemble->uid = $userInside["id"];
        $CtcOrdersExample = Common::assemble(new CtcOrdersExample(), CtcOrdersModel::getCtcCancelOrders($conn, $assemble));
        if($CtcOrdersExample->status != 10){
            Result::error(ErrorEnum::THISORDERHASEXPIRED[$assemble->language]);
        }
        $conn->shiwuStart();
        $assemble->status = 20;
        $assemble->update_time = time();
        $assemble->pay_status = 1;
        if(CtcOrdersModel::upCtcOrdersInside($conn, $assemble) != DigitEnum::ONE){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::OPERATIONFAILED[$assemble->language]);
        }
        $conn->shiwuEnd();
        $userInsideId = UsersModel::getUserInsideId($conn, $CtcOrdersExample->to_uid);
        Vcode::TctSend($userInsideId["phone"],$userInside["phone"],3);
        Result::success(ErrorEnum::PAYMENTSUCCESSFUL[$assemble->language]);
    }
    /**
     *TODO 订单申诉
     * @param $data $id, $mark
     * @return void
     */
    function orderAppeal($data){
        $assemble = Common::assemble(new CtcOrdersExample(), $data);
        if($assemble->id == null){
            Result::error(ErrorEnum::THISORDERHASEXPIRED[$assemble->language]);
        }
        if($assemble->mark == null){
            Result::error(ErrorEnum::PLEASEFILLINTHEAPPEALCONTENT[$assemble->language]);
        }
        $conn = new Conn();
        $userInside = UsersModel::getUserInside($conn, $assemble->token);
        $assemble->uid = $userInside["id"];
        $ctcCancelOrders = CtcOrdersModel::getCtcCancelOrders($conn, $assemble);
        if(!$ctcCancelOrders || $ctcCancelOrders["appeal"] != DigitEnum::ZERO || $ctcCancelOrders["pay_status"] != DigitEnum::TWO){
            Result::error(ErrorEnum::NONACTIONABLEORDERS[$assemble->language]);
        }
        $assemble->appeal = 1;
        $assemble->update_time = time();
        $conn->shiwuStart();
        if(CtcOrdersModel::upCtcOrdersInside($conn, $assemble) != DigitEnum::ONE){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::OPERATIONFAILED[$assemble->language]);
        }
        $conn->shiwuEnd();
        Result::success(ErrorEnum::OPERATIONSUCCESSFUL[$assemble->language]);
    }
    /**
     *TODO 订单驳回
     * @param $data $id, $mark
     * @return void
     */
    function orderReject($data){
        $assemble = Common::assemble(new CtcOrdersExample(), $data);
        if($assemble->id == null){
            Result::error(ErrorEnum::THISORDERHASEXPIRED[$assemble->language]);
        }
        if($assemble->mark == null){
            Result::error(ErrorEnum::PLEASEFILLINTHEAPPEALCONTENT[$assemble->language]);
        }
        $conn = new Conn();
        $userInside = UsersModel::getUserInside($conn, $assemble->token);
        $assemble->to_uid = $userInside["id"];
        $ctcCancelOrders = CtcOrdersModel::getCtcOrdersToUid($conn, $assemble);
        if(!$ctcCancelOrders ||  $ctcCancelOrders["pay_status"] == DigitEnum::TWO){
            Result::error(ErrorEnum::NONACTIONABLEORDERS[$assemble->language]);
        }
        $assemble->pay_status = 2;
        $assemble->status = 10;
        $assemble->update_time = time();
        $conn->shiwuStart();
        if(CtcOrdersModel::upCtcOrdersInside($conn, $assemble) != DigitEnum::ONE){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::OPERATIONFAILED[$assemble->language]);
        }
        $conn->shiwuEnd();
        Result::success(ErrorEnum::OPERATIONSUCCESSFUL[$assemble->language]);
    }
    /**
     *TODO 确认放行
     * @param $userInfo $id pay_password
     * @return void
     */
    function orderRelease($data){
        $assemble = Common::assemble(new CtcOrdersExample(), $data);
        if($assemble->id == null){
            Result::error(ErrorEnum::THISORDERHASEXPIRED[$assemble->language]);
        }
        $conn = new Conn();
        $userInside = UsersModel::getUserPasswordInside($conn, $assemble->token);
        $assemble->to_uid = $userInside["id"];
        $ctcCancelOrders = CtcOrdersModel::getCtcOrdersToUid($conn, $assemble);
        if(!$ctcCancelOrders ||  $ctcCancelOrders["status"] != DigitEnum::TWENTY){
            Result::error(ErrorEnum::NONACTIONABLEORDERS[$assemble->language]);
        }
        if(md5(md5($data["pay_password"])) != $userInside["pay_password"]){
            Result::error(ErrorEnum::SECONDLEVELPAYMENTPASSWORDERROR[$assemble->language]);
        }
        $ctcExample = new CtcExample();
        $ctcExample->id = $ctcCancelOrders["goods_id"];
        $ctcExample->update_time = $ctcExample->finnshed_time = $assemble->update_time =  $assemble->finnshed_time = time();
        $ctcExample->status = 3;
        $assemble->status = 30;
        $conn->shiwuStart();
        //修改订单状态
        $upCtcOrdersInside = CtcOrdersModel::upCtcOrdersInside($conn, $assemble);
        $upCtcInside = CtcModel::upCtcInside($conn, $ctcExample);
        if($upCtcOrdersInside != DigitEnum::ONE || $upCtcInside != DigitEnum::ONE){
            $conn->shiwuRollback();
            Result::error(ErrorEnum::OPERATIONFAILED[$assemble->language]);
        }
        $totalMoney = $ctcCancelOrders["quantity"];
        $UserExample = Common::assemble(new UserExample(), UsersModel::getUserInsideId($conn, $ctcCancelOrders["uid"]));
        //写入余额
        if($ctcCancelOrders["wid"] == 2){
            $balance = $UserExample->money + $totalMoney;
            $editUserMoneyAddInside = UsersModel::editUserMoneyAddInside($conn, $totalMoney, $UserExample);
            $writeFundsMoney = Funds::writeFundsMoney($conn, $UserExample->id, $ctcCancelOrders["sn"], FundsEnum::CTCBUY, $totalMoney, $balance, FundsEnum::CTCBUYZHCN);
            if($editUserMoneyAddInside != DigitEnum::ONE || $writeFundsMoney != DigitEnum::ONE){
                $conn->shiwuRollback();
                Result::error(ErrorEnum::OPERATIONFAILED[$assemble->language]);
            }
        }else if($ctcCancelOrders["wid"] == 6){
            $balance = $UserExample->usdt + $totalMoney;
            $editUserMoneyAddInside = UsersModel::editUserUsdtAddInside($conn, $totalMoney, $UserExample);
            $writeFundsMoney = Funds::writeFundsUsdt($conn, $UserExample->id, $ctcCancelOrders["sn"], FundsEnum::CTCBUY, $totalMoney, $balance, FundsEnum::CTCBUYZHCN);
            if($editUserMoneyAddInside != DigitEnum::ONE || $writeFundsMoney != DigitEnum::ONE){
                $conn->shiwuRollback();
                Result::error(ErrorEnum::OPERATIONFAILED[$assemble->language]);
            }
        }else{
            $balance = $UserExample->myr + $totalMoney;
            $editUserMoneyAddInside = UsersModel::editUserMyrAddInside($conn, $totalMoney, $UserExample);
            $writeFundsMoney = Funds::writeFundsMyr($conn, $UserExample->id, $ctcCancelOrders["sn"], FundsEnum::CTCBUY, $totalMoney, $balance, FundsEnum::CTCBUYZHCN);
            if($editUserMoneyAddInside != DigitEnum::ONE || $writeFundsMoney != DigitEnum::ONE){
                $conn->shiwuRollback();
                Result::error(ErrorEnum::OPERATIONFAILED[$assemble->language]);
            }
        }
        //更新价格
        if($ctcCancelOrders["wid"] == 2){
            $ctcSettingExample = new CtcSettingExample();
            $ctcSettingExample->id = $ctcCancelOrders["wid"];
            $ctcSettingExample->consult_price = $ctcCancelOrders["price"];
            $ctcSettingInit = CtcSettingModel::getCtcSettingInit($conn, $ctcSettingExample->id);
            $up_down_base = $ctcSettingInit["up_down_base"];
            if($up_down_base > 0 && $up_down_base < 1){
                $ctcSettingExample->max_price = $ctcSettingExample->consult_price * (1 + $up_down_base);
                $ctcSettingExample->min_price = $ctcSettingExample->consult_price * (1 - $up_down_base);
            }
            if($ctcSettingExample->consult_price > 0 && $up_down_base > 0){
                if(CtcSettingModel::upCtcSettingInside($conn, $ctcSettingExample) > DigitEnum::ONE){
                    $conn->shiwuRollback();
                    Result::error(ErrorEnum::OPERATIONFAILED[$assemble->language]);
                }
            }
        }
        $conn->shiwuEnd();
        Vcode::TctSend($UserExample->phone,$userInside["phone"],4);
        Result::success(ErrorEnum::OPERATIONSUCCESSFUL[$assemble->language]);
    }
}